package zhoufu.sz.sanxiao_check.ui.adapter.items;

import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import java.util.List;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ItemFireHeaderBinding;
import zhoufu.sz.sanxiao_check.models.DangerModel;

/**
 * Created by hyc on 16-8-4.
 * 项目 ： XTSanXiao_V2
 * 开发 ： hyc
 * 时间 ： 16-8-4 上午11:20
 * 功能 ：
 */

public class FireSafeHeaderItem extends ExpandableHeaderItem<DangerModel, FireSafeChildItem>
        implements View.OnClickListener, View.OnLongClickListener,
        RadioGroup.OnCheckedChangeListener
{

    public ItemFireHeaderBinding _headerBinding;

  private int index;

  public void setIndex(int index) {
    this.index = index;
  }

  @Override
    public boolean equals(Object o)
    {
        if ( o instanceof FireSafeHeaderItem )
        {
            FireSafeHeaderItem item = (FireSafeHeaderItem) o;
            return item.getDB().getID() == this.getDB().getID();
        }
        return false;
    }

    @Override
    protected void setVariable(final FlexibleAdapter adapter,final ViewHolder holder, int position,
                               List payloads)
    {
        super.setVariable(adapter, holder, position, payloads);
        _headerBinding = (ItemFireHeaderBinding) holder._dataBinding;
        _headerBinding.setModel(getDB());
        // 拦截点击事件
        _headerBinding.llFireHeader.setOnClickListener(this);
        _headerBinding.llFireHeader.setOnLongClickListener(this);
        _headerBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isExpanded()) {
                    holder.collapseView(holder.getAdapterPosition());
                }else{
                    holder.expandView(holder.getAdapterPosition());
                }
            }
        });
        _headerBinding.rbCbGone.setOnCheckedChangeListener(this);
      Log.e("-------", "setVariable: "+ position+"--------"+holder.getAdapterPosition());
        //if ( position > 0 )
        //{
        //    position /= 2;
        //}
        //position += 1;
        _headerBinding.tvCount.setText(index+"");
        _headerBinding.clContainer.setChecked(getDB().getIs_Ok() == 1);
    }

    @Override
    public int getLayoutRes()
    {
        return R.layout.item_fire_header;
    }


    @Override
    public void setExpanded(boolean expanded)
    {
        super.setExpanded(expanded);
        if ( _headerBinding != null )
        {
            if ( getDB() != null )
            {

            }
        }
    }

    @Override
    public void onClick(View v)
    {

    }

    @Override
    public boolean onLongClick(View v)
    {
        return true;
    }

    @Override public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.cb_woodiness:
                getDB().setItem_ContentCheck(0);
                break;
            case R.id.cb_iron:
                getDB().setItem_ContentCheck(1);
                break;
            case R.id.cb_cement:
                getDB().setItem_ContentCheck(2);
                break;
            case R.id.cb_other:
                getDB().setItem_ContentCheck(3);
                break;
        }
    }
}
